<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="资金使用时间" prop="usefundsTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.usefundsTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择资金使用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="资金支付方式" prop="usefundsType">
        <el-select v-model="queryParams.usefundsType" placeholder="请选择资金支付方式" clearable size="small">
          <el-option
            v-for="dict in usefundsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资金去向单号" prop="usefundsOrder">
        <el-input
          v-model="queryParams.usefundsOrder"
          placeholder="请输入资金去向单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['publish:usefunds:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usefundsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资金使用记录编号" align="center" prop="id" width="150"/>
      <el-table-column label="资金使用时间" align="center" prop="usefundsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.usefundsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本次使用资金金额" align="center" prop="usefundsNumber" width="200"/>
      <el-table-column label="资金支付方式" align="center" prop="usefundsType" :formatter="usefundsTypeFormat" width="250"/>
      <el-table-column label="资金去向单号" align="center" prop="usefundsOrder" width="220"/>
      <el-table-column label="资金用途说明" align="center" prop="usefundsMessage" width="400"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['publish:usefunds:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['publish:usefunds:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资金用途说明对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资金使用时间" prop="usefundsTime">
          <el-date-picker clearable size="small"
                          v-model="form.usefundsTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择资金使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="本次使用资金金额" prop="usefundsNumber">
          <el-input v-model="form.usefundsNumber" placeholder="请输入本次使用资金金额" />
        </el-form-item>
        <el-form-item label="资金支付方式" prop="usefundsType">
          <el-select v-model="form.usefundsType" placeholder="请选择资金支付方式">
            <el-option
              v-for="dict in usefundsTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资金去向单号" prop="usefundsOrder">
          <el-input v-model="form.usefundsOrder" placeholder="请输入资金去向单号" />
        </el-form-item>
        <el-form-item label="资金用途说明" prop="usefundsMessage">
          <el-input v-model="form.usefundsMessage" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item >
        <el-upload
          ref="upload"
          :limit="1"
          accept=".jpg, .png"
          :action="upload.url"
          :headers="upload.headers"
          :file-list="upload.fileList"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选择资金用途证明文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" :loading="upload.isUploading" @click="submitUpload">上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMyUsefunds, getUsefunds, delUsefunds, addUsefunds, updateUsefunds } from "@/api/publish/usefunds/usefunds";
import { getToken } from "@/utils/auth";

export default {
  name: "Usefunds",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资金用途说明表格数据
      usefundsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 资金支付方式字典
      usefundsTypeOptions: [],
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上传的文件列表
        fileList: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usefundsTime: null,
        usefundsNumber: null,
        usefundsType: null,
        usefundsOrder: null,
        usefundsMessage: null,
        usefundsFilename: null,
        usefundsFilepath: null,
        deptId: null,
        createById: null,
        updateById: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        usefundsTime: [
          { required: true, message: "资金使用时间不能为空", trigger: "blur" }
        ],
        usefundsNumber: [
          { required: true, message: "本次使用资金金额不能为空", trigger: "blur" },
          {
            pattern: /(^[1-9]([0-9]{0,10})?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]{1,2}$)/,
            message: '请输入正确使用金额,可保留两位小数'
          }
        ],
        usefundsType: [
          { required: true, message: "资金支付方式不能为空", trigger: "change" }
        ],
        usefundsOrder: [
          { required: true, message: "资金去向单号不能为空", trigger: "blur" }
        ],
        usefundsMessage: [
          { required: true, message: "资金用途说明不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("usefunds_type").then(response => {
      this.usefundsTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询资金用途说明列表 */
    getList() {
      this.loading = true;
      listMyUsefunds(this.queryParams).then(response => {
        this.usefundsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 资金支付方式字典翻译
    usefundsTypeFormat(row, column) {
      return this.selectDictLabel(this.usefundsTypeOptions, row.usefundsType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        usefundsTime: null,
        usefundsNumber: null,
        usefundsType: null,
        usefundsOrder: null,
        usefundsMessage: null,
        usefundsFilename: null,
        usefundsFilepath: null,
        deptId: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.upload.fileList = [];
      this.title = "添加资金用途说明";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.upload.fileList = [{ name: this.form.fileName, url: this.form.filePath }];
      const id = row.id || this.ids
      getUsefunds(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资金用途说明";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            updateUsefunds(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();

            });
          } else {
            addUsefunds(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除资金用途说明编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUsefunds(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.usefundsFilepath = response.url;
      this.form.usefundsFilename = response.fileName;
      this.msgSuccess(response.msg);
    },
  }
};
</script>
